package xml.handler;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import folder.helper.Folders;
import xml.handler.LayoutDataSet;
import layoutitems.ImageElement;

public class LayoutSaxHandler extends DefaultHandler {

	// ===========================================================
	// Fields
	// ===========================================================
	private boolean in_RootTag = false;
	private boolean in_LayoutItemTag = false;
	private boolean in_NameTag = false;
	private boolean in_LinkTag = false;
	private boolean in_BlockedTag = false;
	private boolean in_xPositionTag = false;
	private boolean in_yPositionTag = false;
	private boolean in_layerPositionTag = false;
	private LayoutDataSet LayoutDataSet = new LayoutDataSet();

	// ===========================================================
	// Getter & Setter
	// ===========================================================
	public LayoutDataSet getParsedData() {
		return this.LayoutDataSet;
	}

	// ===========================================================
	// Methods
	// ===========================================================
	@Override
	public void startDocument() throws SAXException {
		this.LayoutDataSet = new LayoutDataSet();
	}

	@Override
	public void endDocument() throws SAXException {
		// Nothing to do
	}

	/**
	 * Gets be called on opening tags like: <tag> Can provide attribute(s), when
	 * xml was like: <tag attribute="attributeValue">
	 */
	@Override
	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException {
		// System.out.println(namespaceURI+"; ln: "+
		// localName+"; qName: "+qName+"; atts: "+atts);
		if (qName.equals("root")) {
			this.in_RootTag = true;
		} else if (qName.equals("layoutItem")) {
			this.in_LayoutItemTag = true;
			LayoutDataSet.setCurrentImageElement(new ImageElement());
		} else if (qName.equals("name")) {
			this.in_NameTag = true;
		} else if (qName.equals("link")) {
			this.in_LinkTag = true;
		} else if (qName.equals("blocked")) {
			this.in_BlockedTag = true;
		} else if (qName.equals("x_position")) {
			this.in_xPositionTag = true;
		} else if (qName.equals("y_position")) {
			this.in_yPositionTag = true;
		} else if (qName.equals("layer_position")) {
			this.in_layerPositionTag = true;
		}
	}

	/**
	 * Gets be called on closing tags like: </tag>
	 */
	@Override
	public void endElement(String namespaceURI, String localName, String qName)
			throws SAXException {
		if (qName.equals("root")) {
			this.in_RootTag = false;
		} else if (qName.equals("layoutItem")) {
			this.in_LayoutItemTag = false;
			LayoutDataSet.addCurrentImageElement();
		} else if (qName.equals("name")) {
			this.in_NameTag = false;
		} else if (qName.equals("link")) {
			this.in_LinkTag = false;
		} else if (qName.equals("blocked")) {
			this.in_BlockedTag = false;
		} else if (qName.equals("x_position")) {
			this.in_xPositionTag = false;
		} else if (qName.equals("y_position")) {
			this.in_yPositionTag = false;
		} else if (qName.equals("layer_position")) {
			this.in_layerPositionTag = false;
		}
	}

	/**
	 * Gets be called on the following structure: <tag>characters</tag>
	 */
	@Override
	public void characters(char ch[], int start, int length) {
		// System.out.println(new String(ch, start, length));
		if (this.in_NameTag) {
			if (LayoutDataSet.getCurrentImageElement() == null) {
				LayoutDataSet.setCurrentImageElement(new ImageElement());
			}
			LayoutDataSet.getCurrentImageElement().setName(
					new String(ch, start, length));
		} else if (this.in_LinkTag) {
			if (LayoutDataSet.getCurrentImageElement() == null) {
				LayoutDataSet.setCurrentImageElement(new ImageElement());
			}

			String picturePath = new String(ch, start, length);
			if (picturePath.contains("/")) {
				picturePath.replace("/", Folders.SEP);
			} else if (picturePath.contains("\\")) {
				picturePath = picturePath.replace("\\", Folders.SEP);
			}
			LayoutDataSet.getCurrentImageElement().setPicture(picturePath);
		} else if (this.in_BlockedTag) {
			if (LayoutDataSet.getCurrentImageElement() == null) {
				LayoutDataSet.setCurrentImageElement(new ImageElement());
			}
			if ((new String(ch, start, length)).equals("true")) {
				LayoutDataSet.getCurrentImageElement().setBlocked(true);
			} else if ((new String(ch, start, length)).equals("false")) {
				LayoutDataSet.getCurrentImageElement().setBlocked(false);
			}
		} else if (this.in_xPositionTag) {
			if (LayoutDataSet.getCurrentImageElement() == null) {
				LayoutDataSet.setCurrentImageElement(new ImageElement());
			}
			LayoutDataSet.getCurrentImageElement().setPosX(
					Integer.parseInt(new String(ch, start, length)));
		} else if (this.in_yPositionTag) {
			if (LayoutDataSet.getCurrentImageElement() == null) {
				LayoutDataSet.setCurrentImageElement(new ImageElement());
			}
			LayoutDataSet.getCurrentImageElement().setPosY(
					Integer.parseInt(new String(ch, start, length)));
		} else if (this.in_layerPositionTag) {
			// if (LayoutDataSet.getCurrentImageElement() == null) {
			// LayoutDataSet.setCurrentImageElement(new ImageElement());
			// }
			// LayoutDataSet.getCurrentImageElement().setPosY(Integer.parseInt(new
			// String(ch, start, length)));
		}
	}
}
